package com.zqwh.storemonitor.activity;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zqwh.storemonitor.R;
import com.zqwh.storemonitor.domain.Shop;

import java.util.List;

import static com.zqwh.storemonitor.Config.STORE_SPAN_COUNT;

public class StoreAdapter extends RecyclerView.Adapter<StoreAdapter.LocalViewHolder>{
    private Context context;
    private List<Shop> shopList;

    public StoreAdapter(Context context, List<Shop> shopList) {
        this.context = context;
        this.shopList = shopList;
    }

    /**
     * 定义RecyclerView选项单击事件的回调接口
     */
    public interface OnItemClickListener{
        void onItemClick(LocalViewHolder holder, int position, Shop shop);
        void onSoundSwitchClick(LocalViewHolder holder, int position, Shop shop);
        void onVoiceSwitchClick(LocalViewHolder holder, int position, Shop shop);
        void onLockSwitchClick(LocalViewHolder holder, int position, Shop shop);
    }

    private OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public LocalViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        int resource = R.layout.item_shop_1;
        if (STORE_SPAN_COUNT == 1){
            resource = R.layout.item_shop_1;
        }else if (STORE_SPAN_COUNT == 2){
            resource = R.layout.item_shop_2;
        }
        View itemView = LayoutInflater.from(context).inflate(resource, viewGroup, false);
        return new LocalViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final LocalViewHolder holder, final int position) {
        final Shop shop = shopList.get(position);
        String name = shop.getAlias();
        holder.textView.setText(shop.getAlias());
        if (shop.getMonitorOnline()){
            holder.textView.setText(name);
        } else {
            holder.textView.setText(name+(shop.getMonitorOnline()?"":"（离线）"));
        }
        shop.setRepresentMonitorSv(holder.surfaceView);
        if (shop.isRepresentMonitorSoundOn()){
            holder.imgSoundSwitch.setImageDrawable(context.getDrawable(R.mipmap.sound_on));
            holder.imgSoundSwitch.setAlpha((float) 1);
        } else {
            holder.imgSoundSwitch.setImageDrawable(context.getDrawable(R.mipmap.sound_on));
            holder.imgSoundSwitch.setAlpha((float) 0.5);
        }

        if (shop.isRepresentMonitorVoiceOn()){
            holder.imgVoiceSwitch.setImageDrawable(context.getDrawable(R.mipmap.voice_on));
            holder.imgVoiceSwitch.setAlpha((float) 1);
        }else {
            holder.imgVoiceSwitch.setImageDrawable(context.getDrawable(R.mipmap.voice_on));
            holder.imgVoiceSwitch.setAlpha((float) 0.5);
        }

        if (shop.isOutAllow()){
            holder.imgLockSwitch.setImageDrawable(context.getDrawable(R.mipmap.lock_on));
            holder.imgLockSwitch.setAlpha((float) 0.5);
        }else {
            holder.imgLockSwitch.setImageDrawable(context.getDrawable(R.mipmap.lock_off));
            holder.imgLockSwitch.setAlpha((float)1);
        }

        //程序执行到此，会去执行具体实现的onItemClick()方法
        if (onItemClickListener != null){
            holder.surfaceView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(holder, position, shop);
                }
            });
            holder.imgSoundSwitch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onSoundSwitchClick(holder, position, shop);
                }
            });
            holder.imgVoiceSwitch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onVoiceSwitchClick(holder, position, shop);
                }
            });
            holder.imgLockSwitch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onLockSwitchClick(holder, position, shop);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return shopList.size();
    }

    static class LocalViewHolder extends RecyclerView.ViewHolder{
        SurfaceView surfaceView;
        TextView textView;
        ImageView imgSoundSwitch;
        ImageView imgVoiceSwitch;
        ImageView imgLockSwitch;

        public LocalViewHolder(@NonNull View itemView) {
            super(itemView);
            surfaceView = itemView.findViewById(R.id.sv_realplay);
            textView = itemView.findViewById(R.id.tv_shop_name);
            imgSoundSwitch = itemView.findViewById(R.id.img_sound_switch);
            imgVoiceSwitch = itemView.findViewById(R.id.img_voice_switch);
            imgLockSwitch = itemView.findViewById(R.id.img_direction_switch);
        }
    }
}
